package com.autonavi.yunda.yunji.controller.request;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.core.model.dto.DynamicConfigDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;

import javax.validation.constraints.NotBlank;

/**
 * @author cizhong.wcs
 * @date 2022/6/27 16:25
 */
@Data
public class DynamicConfigRequest {

    private Long id;
    private String projectId;
    @NotBlank
    private String keyName;
    @NotBlank
    private String value;
    private String description;
    private String operator;

    public DynamicConfigDTO toAddDTO() {
        AmapPreconditions.checkArgumentForUser(projectId != null, "projectId不能为空");
        return DynamicConfigDTO.builder()
                .projectId(projectId)
                .keyName(keyName)
                .value(value)
                .description(description)
                .createUser(operator)
                .updateUser(operator)
                .build();

    }

    public UpdateWrapper<DynamicConfigDTO> toUpdateWrapper() {
        AmapPreconditions.checkArgumentForUser(id != null, "id不能为空");
        QueryWrapper<DynamicConfigDTO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        UpdateWrapper<DynamicConfigDTO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        if (Strings.isNotBlank(keyName)) {
            updateWrapper.set("key_name", keyName);
        }
        if (Strings.isNotBlank(value)) {
            updateWrapper.set("value", value);
        }
        if (Strings.isNotBlank(description)) {
            updateWrapper.set("description", description);
        }
        if (Strings.isNotBlank(operator)) {
            updateWrapper.set("update_user", operator);
        }
        return updateWrapper;
    }
}
